package com.ph.shopping.facade.member.service.dbservice.facade.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ph.shopping.common.core.customenum.MemberEnum;
import com.ph.shopping.common.core.customenum.OuterResultEnum;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.SmsCodeType;
import com.ph.shopping.common.core.exception.BizException;
import com.ph.shopping.common.core.other.smssend.util.SmsSendUtil;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.container.ParamVerifyUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.rsa.MD5;
import com.ph.shopping.facade.customfactory.CustomBeanRule;
import com.ph.shopping.facade.customfactory.facade.ICustomBeanFactory;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.mapper.MemberMapper;
import com.ph.shopping.facade.member.dto.CertificatesAuthDto;
import com.ph.shopping.facade.member.dto.MemberInfoDto;
import com.ph.shopping.facade.member.dto.MemberLoginDto;
import com.ph.shopping.facade.member.dto.MemberPageDto;
import com.ph.shopping.facade.member.dto.MemberPasswordDto;
import com.ph.shopping.facade.member.dto.MerberCertificatesDto;
import com.ph.shopping.facade.member.dto.PayPasswordDto;
import com.ph.shopping.facade.member.dto.RegisterMemberDto;
import com.ph.shopping.facade.member.entity.IdcertificatesInfo;
import com.ph.shopping.facade.member.entity.Member;
import com.ph.shopping.facade.member.entity.TradersPassword;
import com.ph.shopping.facade.member.service.ISmsCodeSendService;
import com.ph.shopping.facade.member.service.dbservice.facade.ICretificateAuthDbService;
import com.ph.shopping.facade.member.service.dbservice.facade.IMemberDbService;
import com.ph.shopping.facade.member.service.user.LoginRegisterCheckService;
import com.ph.shopping.facade.member.service.user.response.CheckResponse;
import com.ph.shopping.facade.member.service.user.response.Content;
import com.ph.shopping.facade.member.vo.MemberPhoneUpdataVo;
import com.ph.shopping.facade.member.vo.MemberVo;

@Service
public class MemberDbService implements IMemberDbService{
	
	private static final Logger log = LoggerFactory.getLogger(MemberDbService.class);

	@Autowired
	private MemberMapper memeberMapper;

	@Autowired
	private ICustomBeanFactory<CustomBeanRule> customBeanFactory;
	/**
	 * 身份证件认证
	 */
	@Autowired
	private ICretificateAuthDbService authService;
	/**
	 * 短信发送
	 */
	@Autowired
	private ISmsCodeSendService smsCodeService;
	/**
	 * 登录注册校验服务
	 */
	@Autowired
	private LoginRegisterCheckService checkService;
	
	@Override
	public Result queryMemberListByPage(MemberPageDto pageInfo) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = null;
		try {
			map = customBeanFactory.beanToMap(pageInfo);
		} catch (Exception e) {
			log.error("MemberPageDto to map error", e);
		}
		if (map != null) {
			PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
			List<MemberVo> list = memeberMapper.selectMemberByPage(map);
			PageInfo<MemberVo> page = new PageInfo<MemberVo>(list);
			result.setCount(page.getTotal());
			result.setData(page.getList());
			result = DubboResult.getResultBySuccess(result);

		}
		return result;
	}

	@Override
	public Result queryMemberList(MemberPageDto dto) {
		Result result = DubboResult.getDefResultByImpl();
		Member member = getMemberByMemberPageDto(dto);
		if (member != null) {
			List<Member> list = memeberMapper.selectMemberListByMember(member);
			if (ParamVerifyUtil.objIsNotNull(list)) {
				result.setData(list);
				result.setCount(list.size());
				result = DubboResult.getResultBySuccess(result);

			}
		}
		return result;
	}

	private Member getMemberByMemberPageDto(MemberPageDto dto) {
		Member member = new Member();
		BeanUtils.copyProperties(dto, member);
		return member;
	}

	@Override
	@Transactional
	public Result addMember(RegisterMemberDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		// 判断北京方是否已存在会员数据，存在则直接返回成功
		CheckResponse response = checkService.registerCheck(dto.getMeber().getMemberPhone());
		if (response != null && OuterResultEnum.SUCCESS.getCode().equals(response.getCode())) {
			return DubboResult.getResultBySuccess(result);

		}
		String pwd = SmsSendUtil.getSmsCode();
		Member mem = memeberMapper.selectMemberInfoByMobile(dto.getMeber().getMemberPhone());
		if (mem != null && mem.getId() != null) {
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			result.setMessage("当前会员已存在，不能重复添加");
			return result;
		}
		Member member = dto.getMeber();
		member.setMemberPwd(pwdMd5Str(pwd));
		member.setStatus(MemberEnum.MEMBER_NORMAL.getCode());
		member.setCertification(MemberEnum.NO_CERTIFICATION.getCode());
		// 默认为会员状态
		member.setIsMarketing(MemberEnum.IS_MARKETING_BYNO.getCode());
		member.setCreateTime(new Date());
		if (member.getLevel() == null) {
			member.setLevel(MemberEnum.MEMBER_GENERAL.getCode());
		}
		memeberMapper.insertMember(member);
		// 向会员积分表新增数据
		boolean flag = insertIntoScoreData(member.getId());
		if (!flag) {
			throw new BizException("新增会员时向会员积分表新增数据失败");
		}
		result = smsCodeService.sendSmsCodeByCustomMsg(dto.getMeber().getMemberPhone(),
				SmsCodeType.SEND_USER_PWD.getCodeType(), pwd);
		if (result == null || !ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
			log.info("给用户发送短信密码失败");
			throw new BizException("给用户发送短信密码失败");
		}
		return DubboResult.getResultBySuccess(result);
	}

	/**
	 * 
	* @Title: insertIntoScoreData  
	* @Description: 新增会员时 向会员积分数据表新增记录 
	* @param @param memberId
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	@Transactional
	private boolean insertIntoScoreData(Long memberId) {
		if (memberId != null) {
			Map<String, Object> scoreMap = ContainerUtil.map();
			scoreMap.put("memberId", memberId);
			scoreMap.put("createTime", new Date());
			scoreMap.put("updateTime", new Date());
			memeberMapper.insertMemberScoreData(scoreMap);
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public Result updateMember(MemberInfoDto memberUpdateDto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Member member = getMemberByInfoDto(memberUpdateDto);
		if (member != null) {
			member.setMemberPhone(null);//不允许修改手机号
			member.setUpdateTime(new Date());
			memeberMapper.updateMember(member);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result deleteMember(Integer id) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		if (id != null) {
			memeberMapper.deleteMember(id);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result batchUpdateMembers(List<MemberInfoDto> list) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		List<Member> params = ContainerUtil.aList(list.size());
		for (MemberInfoDto dto : list) {
			Member member = getMemberByInfoDto(dto);
			if (member != null) {
				params.add(member);
			}
		}
		if (!params.isEmpty()) {
			memeberMapper.batchUpdateMembers(params);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	/**
	 * 
	* @Title: getMemberByInfoDto  
	* @Description:   
	* @param @param dto
	* @param @return    参数  
	* @return Member    返回类型  
	* @throws
	 */
	private Member getMemberByInfoDto(MemberInfoDto dto) {
		Member member = new Member();
		BeanUtils.copyProperties(dto, member);
		return member;
	}

	@Override
	@Transactional
	public Result batchDeleteMembers(List<Integer> list) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		if (ParamVerifyUtil.objIsNotNull(list)) {
			memeberMapper.batchDeleteMembers(list);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result queryMemberInfoByMobile(String mobile) {
		Result result = DubboResult.getDefResultByImpl();
		Member member = memeberMapper.selectMemberInfoByMobile(mobile);
		if (member != null && StringUtils.isNotBlank(member.getMemberPhone())) {

			result.setData(member);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result registerMember(RegisterMemberDto dto) {
		Member member = dto.getMeber();
		// 判断北京方是否已存在会员数据，存在则直接返回成功
		CheckResponse response = checkService.registerCheck(member.getMemberPhone());
		if (response != null && OuterResultEnum.SUCCESS.getCode().equals(response.getCode())) {
			Result result = DubboResult.getResultBySuccess();
			result.setData(member);
			return result;

		}
		return insertMermberInfo(member, dto.getTradersPassword());
	}
	/**
	 * 
	* @Title: insertMermber  
	* @Description:新增会员数据  
	* @param @param member
	* @param @param tpd
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	private Result insertMermberInfo(Member member, TradersPassword tpd) {
		Result result = DubboResult.getResultBySuccess();
		Member mem = memeberMapper.selectMemberInfoByMobile(member.getMemberPhone());
		if (mem != null && mem.getId() != null) {
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			result.setMessage("当前会员已存在");
			return result;
		}
		member.setStatus(MemberEnum.MEMBER_NORMAL.getCode());
		member.setCertification(MemberEnum.NO_CERTIFICATION.getCode());
		// 默认为会员状态
		member.setIsMarketing(MemberEnum.IS_MARKETING_BYNO.getCode());
		String pwd = member.getMemberPwd();
		member.setMemberPwd(pwdMd5Str(pwd));
		member.setCreateTime(new Date());
		if (member.getLevel() == null) {
			member.setLevel(MemberEnum.MEMBER_GENERAL.getCode());
		}
		memeberMapper.insertMember(member);
		Long id = member.getId();
		// 向会员积分表新增数据
		boolean flag = insertIntoScoreData(id);
		if (!flag) {
			throw new BizException("新增会员时向会员积分表新增数据失败");
		}
		if (tpd != null && StringUtils.isNotBlank(tpd.getPayPwd())) {
			if (id == null) {
				throw new BizException("添加会员数据返回主键失败,无法添加支付密码");
			}
			tpd.setRelatedId(id);
			if (!addTradersPassword(tpd)) {
				throw new BizException("新增会员支付密码失败");
			}
		}
		return result;
	}
	/**
	 * 
	* @Title: addTradersPassword  
	* @Description: 新增支付密码  
	* @param @param tradersPassword
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	@Transactional
	private boolean addTradersPassword(TradersPassword tradersPassword) {
		if (tradersPassword != null) {
			Map<String, Object> map = ContainerUtil.map();
			map.put("relatedId", tradersPassword.getRelatedId());
			map.put("customerType", tradersPassword.getCustomerType());
			TradersPassword paypwdinfo = memeberMapper.selectUserPayPwd(map);
			if (paypwdinfo != null && StringUtils.isNotBlank(paypwdinfo.getPayPwd())) {
				log.warn("支付密码已存在不能重复新增");
				return false;
			} 
			String pwd = tradersPassword.getPayPwd();
			tradersPassword.setPayPwd(pwdMd5Str(pwd));
			tradersPassword.setCreateTime(new Date());
			memeberMapper.insertTradersPassword(tradersPassword);
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public Result memberAuthentication(MemberInfoDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Member member = getMemberByInfoDto(dto);
		if (member != null) {
			member.setUpdateTime(new Date());
			member.setCertification(MemberEnum.CERTIFICATION.getCode());
			member.setMemberPhone(null);
			memeberMapper.updateMember(member);
			List<MerberCertificatesDto> certificatesDto = dto.getCertificatesDto();
			insertCertificatesInfo(certificatesDto);

			boolean flag = idCretificateAuth(member.getMemberName(), member.getIdCard(), member.getId());
			if (!flag) {
				throw new BizException("证件认证失败");
			}
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}
	/**
	 * 
	* @Title: idCretificateAuth  
	* @Description: 证件认证 
	* @param @param name
	* @param @param idCode
	* @param @param userId
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	private boolean idCretificateAuth(String name, String idCode,Long userId){
		CertificatesAuthDto dto = new CertificatesAuthDto();
		dto.setName(name);
		dto.setIdCode(idCode);
		dto.setUserId(userId);
		Result result = authService.idCretificateAuth(dto);
		return result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode());
	}
	
	private boolean insertCertificatesInfo(List<MerberCertificatesDto> list) {
		if (ParamVerifyUtil.objIsNotNull(list)) {
			List<IdcertificatesInfo> infos = ContainerUtil.aList(list.size());
			for (MerberCertificatesDto dto : list) {
				IdcertificatesInfo info = new IdcertificatesInfo();
				BeanUtils.copyProperties(dto, info);
				info.setCreateTime(new Date());
				infos.add(info);
			}
			if (!infos.isEmpty()) {
				memeberMapper.insertIdcertificates(infos);
			}
		}
		return true;
	}
	
	@Override
	@Transactional
	public Result memberFrozen(MemberInfoDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Member member = getMemberByInfoDto(dto);
		if (member != null) {
			member.setUpdateTime(new Date());
			member.setStatus(MemberEnum.MEMBER_FROZEN.getCode());
			member.setMemberPhone(null);
			memeberMapper.updateMember(member);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result memberUpdatePassword(MemberPasswordDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		// 修改登录密码
		Member member = memeberMapper.selectMemberInfoByMobile(dto.getMemberPhone());
		if (member == null || StringUtils.isBlank(member.getMemberPhone())) {
			result.setCode(ResultEnum.MEMBERUSER_NON_EXISTENT.getCode());
			result.setMessage(ResultEnum.MEMBERUSER_NON_EXISTENT.getMsg());
		} else if (MemberEnum.MEMBER_FROZEN.getCode().equals(member.getStatus())) {
			result.setCode(ResultEnum.MEMBER_FROZEN.getCode());
			result.setMessage(ResultEnum.MEMBER_FROZEN.getMsg());
		} else {
			String pwd = pwdMd5Str(dto.getNewPassword());
			member.setMemberPwd(pwd);
			member.setUpdateTime(new Date());
			member.setMemberPhone(null);
			memeberMapper.updateMember(member);
			DubboResult.getResultBySuccess(result);
		}
		return result;
	}
	/**
	 * 
	* @Title: updateTradersPassword  
	* @Description: 修改支付密码
	* @param @param phone
	* @param @param vpwd
	* @param @param newPwd
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	private Result updateTradersPassword(PayPasswordDto dto) {
		Map<String, Object> map = ContainerUtil.map();
		map.put("relatedId", dto.getRelatedId());
		map.put("customerType", dto.getCustomerType());
		map.put("payPwd", pwdMd5Str(dto.getNewPassword()));
		map.put("updateUser", dto.getUpdateUser());
		memeberMapper.updadtePayPwd(map);
		return DubboResult.getResultBySuccess();
	}
	/**
	 * 
	* @Title: isPwdEqual  
	* @Description: 判断密码是否相等  
	* @param @param tagartPwd
	* @param @param pwd
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	private boolean isPwdEqual(String resouse, String target) {
		
		return resouse != null && resouse.equals(target);
	}
	/**
	 * 
	* @Title: pwdMd5Str  
	* @Description: 密码加密  
	* @param @param pwd
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	private String pwdMd5Str(String pwd) {

		return MD5.getMD5Str(pwd);
	}
	
	@Override
	@Transactional
	public Result backMemberPassword(MemberPasswordDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		String mobile = dto.getMemberPhone();
		Member member = memeberMapper.selectMemberInfoByMobile(mobile);
		if (member == null || StringUtils.isBlank(member.getMemberPhone())) {
			log.warn("重置密码会员不存在");
			return DubboResult.getResultByEnum(ResultEnum.MEMBERUSER_NON_EXISTENT);
		} else if (MemberEnum.MEMBER_FROZEN.getCode().equals(member.getStatus())) {
			return DubboResult.getResultByEnum(ResultEnum.MEMBER_FROZEN);
		}
		boolean flag = resetMemberPwd(member, dto.getNewPassword());
		if (flag) {
			result = DubboResult.getResultBySuccess();
		}
		return result;
	}

	/**
	 * 
	* @Title: resetMemberPwd  
	* @Description: 重置会员密码  
	* @param @param member
	* @param @param newPwd
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	private boolean resetMemberPwd(Member member, String newPwd) {
		boolean flag = false;
		if(member != null && member.getId() != null){
			Member inmember = new Member();
			String pwd = pwdMd5Str(newPwd);
			inmember.setMemberPwd(pwd);
			inmember.setUpdateTime(new Date());
			inmember.setId(member.getId());
			member.setMemberPhone(null);
			memeberMapper.updateMember(inmember);
			flag = true;
		}
		return flag;
	}
	
	@Override
	@Transactional
	public Result memberRelievefrozen(MemberInfoDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Member member = getMemberByInfoDto(dto);
		if (member != null) {
			member.setUpdateTime(new Date());
			member.setStatus(MemberEnum.MEMBER_NORMAL.getCode());
			member.setMemberPhone(null);
			memeberMapper.updateMember(member);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result memberVerifyLogin(MemberLoginDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_USER_MISMATCH);
		Member member = memeberMapper.selectMemberInfoByMobile(dto.getPhone());
		String pwd = pwdMd5Str(dto.getPassword());
		boolean flag = false;// 会员存在且密码为空时标记（为true 时则表示用户存在密码为空此时则修改密码操作）
		if (member != null) {
			if(MemberEnum.MEMBER_FROZEN.getCode().equals(member.getStatus())){
				return DubboResult.getResultByEnum(ResultEnum.MEMBER_FROZEN);
			}
			if (StringUtils.isNotBlank(member.getMemberPhone())) {
				flag = StringUtils.isBlank(member.getMemberPwd());
				if (!flag) {
					if (member.getMemberPwd().equals(pwd)) {
						result.setData(member);
						return DubboResult.getResultBySuccess(result);
					} else {
						return DubboResult.getResultByEnum(ResultEnum.MEMBER_PWD_MISMATCH);
					}
				}
			}
		}
		if (member == null || flag) {
			CheckResponse response = checkService.loginCheck(dto.getPhone(), dto.getPassword());
			if (response != null && !OuterResultEnum.USER_NOEXISTS.getCode().equals(response.getCode())) {
				String code = response.getCode();
				if (OuterResultEnum.SUCCESS.getCode().equals(code)) {
					if (flag) {
						boolean updateFlag = resetMemberPwd(member, pwd);
						if (!updateFlag) {
							throw new BizException("修改会员密码错误");
						}
						result.setData(member);
						return DubboResult.getResultBySuccess(result);
					} else {
						Content content = response.getContent();
						if (content != null && StringUtils.isNotBlank(content.getPhone())) {
							Member meber = new Member();
							meber.setCreateTime(new Date());
							meber.setMemberPhone(content.getPhone());
							meber.setMemberName(content.getName());
							meber.setIdCard(content.getCard());
							meber.setMemberPwd(pwd);
							result.setData(meber);
							result = insertMermberInfo(meber, null);
						} else {
							throw new BizException("新增会员数据同步返回错误");
						}
					}
				} else if (OuterResultEnum.PWD_ERROR.getCode().equals(code)) {
					return DubboResult.getResultByEnum(ResultEnum.MEMBER_PWD_MISMATCH);
				}
			}
		}
		return result;
	}

	@Override
	@Transactional
	public Result addMemberPayPwd(TradersPassword tradersPassword) {
		Result result = DubboResult.getResultBySuccess();
		if (!addTradersPassword(tradersPassword)) {
			result.setCode(ResultEnum.MEMBER_FAILURE.getCode());
			result.setMessage("添加支付密码失败");
		}
		return result;
	}

	@Override
	public Result queryMemberPayPwdByMobile(String mobile) {
		Result result = DubboResult.getDefResultByImpl();
		TradersPassword paypwd = memeberMapper.selectUserPayPwdByPhone(mobile);
		if(paypwd != null){
			result.setData(paypwd);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result queryPayPwdInfo(PayPasswordDto dto) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = ContainerUtil.map();
		map.put("relatedId", dto.getRelatedId());
		map.put("customerType", dto.getCustomerType());
		TradersPassword paypwdinfo = memeberMapper.selectUserPayPwd(map);
		if(paypwdinfo != null){
			result.setData(paypwdinfo);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result updatePayPassword(PayPasswordDto dto) {
		
		return updateTradersPassword(dto);
	}

	@Override
	@Transactional
	public Result updateMemberPhone(MemberPhoneUpdataVo vo) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		String phone = vo.getOrigPhone();
		Member member = memeberMapper.selectMemberInfoByMobile(phone);
		if (member == null || StringUtils.isBlank(member.getMemberPhone())) {
			return DubboResult.getResultByEnum(ResultEnum.MEMBERUSER_NON_EXISTENT);
		} else if (MemberEnum.MEMBER_FROZEN.getCode().equals(member.getStatus())) {
			return DubboResult.getResultByEnum(ResultEnum.MEMBER_FROZEN);
		}
		Member newMember = memeberMapper.selectMemberInfoByMobile(vo.getNewPhone());
		if (newMember != null && StringUtils.isNotBlank(newMember.getMemberPhone())) {
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			result.setMessage("新手机号已存在");
			return result;
		}
		TradersPassword paypwdInfo = memeberMapper.selectUserPayPwdByPhone(phone);
		if (paypwdInfo == null || !isPwdEqual(paypwdInfo.getPayPwd(), pwdMd5Str(vo.getPayPwd()))) {
			result.setCode(ResultEnum.MEMBER_NATIVE_PWD_MISMATCH.getCode());
			result.setMessage("支付密码错误");
			return result;
		}
		Member updateInfo = new Member();
		updateInfo.setId(member.getId());
		updateInfo.setUpdateTime(new Date());
		updateInfo.setMemberPhone(vo.getNewPhone());
		memeberMapper.updateMember(updateInfo);
		DubboResult.getResultBySuccess(result);
		return result;
	}
	
	@Override
	public Result verifyPhoneIsExists(String phone) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_USER_MISMATCH);
		int num = memeberMapper.selectMemberNumByPhone(phone);
		if (num > 0) {
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}
	
}
